﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

using System.Drawing;

using MPF.EiF;

namespace EquaShare.Klase
{
    [Serializable]
    public class KljucVrijednost<K, V>
    {
        public K Key { get; set; }

        public V Value { get; set; }

        public KljucVrijednost() { }

        public KljucVrijednost(K kljuc, V vrijednost)
        {
            Key = kljuc;
            Value = vrijednost;
        }
    }

    [Serializable]
    public class SerijalizabilnePostavke
    {
        [XmlElement("Tip_fonta")]
        public String FONT { get; set; }

        [XmlElement("Velicina_fonta")]
        public int FONTSIZE { get; set; }

        [XmlElement("Koristi_se_osluskivac")]
        public bool USELISTENER { get; set; }

        [XmlElement("Keybind_formula")]
        public String OKIDAC { get; set; }

        [XmlElement("Izlaz_uz_potvrdu")]
        public bool ASKONCLOSE { get; set; }

        [XmlElement("Autosave_aktiviran")]
        public bool AUTOSAVEENABLED { get; set; }

        [XmlElement("Vrijeme_autosavea")]
        public int AUTOSAVETIME_SEC { get; set; }

        [XmlElement("Keybindings")]
        public List<KljucVrijednost<String, String>> STRINGMAPS { get; set; }

        [XmlElement("Insertion_keys")]
        public List<KljucVrijednost<String, STRUKTURE>> SHORTCUTS { get; set; }

        [XmlElement("Replace_aktiviran")]
        public bool REPLACEENABLED { get; set; }

        public SerijalizabilnePostavke()
        {

        }

        private List<KljucVrijednost<String, String>> convert_to(List<KeyValuePair<String, String>> f)
        {
            List<KljucVrijednost<String, String>> novi = new List<KljucVrijednost<String, String>>();
            foreach (KeyValuePair<String, String> kvp in f)
                novi.Add(new KljucVrijednost<string, string>(kvp.Key, kvp.Value));
            return novi;
        }

        private List<KljucVrijednost<String, STRUKTURE>> convert_to(List<KeyValuePair<String, STRUKTURE>> f)
        {
            List<KljucVrijednost<String, STRUKTURE>> novi = new List<KljucVrijednost<String, STRUKTURE>>();
            foreach (KeyValuePair<String, STRUKTURE> kvp in f)
                novi.Add(new KljucVrijednost<string, STRUKTURE>(kvp.Key, kvp.Value));
            return novi;
        }

        public void loadFromStatic()
        {
            FONT = Postavke.FONTNAME;
            FONTSIZE = Postavke.FONTSIZE;
            USELISTENER = Postavke.USELISTENER;
            OKIDAC = Postavke.OKIDAC;
            ASKONCLOSE = Postavke.ASKONCLOSE;
            AUTOSAVEENABLED = Postavke.AUTOSAVEENABLED;
            AUTOSAVETIME_SEC = Postavke.AUTOSAVETIME_SEC;
            STRINGMAPS = convert_to(Postavke.STRINGMAPS.ToList());
            SHORTCUTS = convert_to(Postavke.SHORTCUTS.ToList());
            REPLACEENABLED = Postavke.REPLACEENABLED;
        }

        public void loadToStatic()
        {
            Postavke.FONTNAME = FONT;
            Postavke.FONTSIZE = FONTSIZE;
            Postavke.USELISTENER = USELISTENER;
            Postavke.OKIDAC = OKIDAC;
            Postavke.ASKONCLOSE = ASKONCLOSE;
            Postavke.AUTOSAVEENABLED = AUTOSAVEENABLED;
            Postavke.AUTOSAVETIME_SEC = AUTOSAVETIME_SEC;
            Postavke.STRINGMAPS = STRINGMAPS.ToDictionary(p => p.Key, p => p.Value);
            Postavke.SHORTCUTS = SHORTCUTS.ToDictionary(p => p.Key, p => p.Value);
            Postavke.REPLACEENABLED = REPLACEENABLED;
        }
    }

    public static class Postavke
    {
        private static string settingsFileName = "config.xml";

        public static Font FONT { get { return new Font(FONTNAME, FONTSIZE); } }
        public static String FONTNAME;
        public static int FONTSIZE;
        public static bool USELISTENER;
        public static String OKIDAC;
        public static bool ASKONCLOSE;
        public static bool AUTOSAVEENABLED;
        public static int AUTOSAVETIME_SEC;
        public static Dictionary<String, String> STRINGMAPS; // (sta mapiramo, u sta mapiramo)
        public static Dictionary<String, STRUKTURE> SHORTCUTS; // (cime mapiramo, sta )
        public static bool REPLACEENABLED;

        public static void LOAD_DEFAULTS()
        {
            FONTSIZE = 12;
            FONTNAME = "SegoeUI";
            USELISTENER = true;
            OKIDAC = "§F"; // TODO
            ASKONCLOSE = false;
            AUTOSAVEENABLED = false;
            AUTOSAVETIME_SEC = 0;

            STRINGMAPS = new Dictionary<String, String>() { 
            {"*","·"},
            {"!=","≠"},
            {">=","≥"},
            {"<=","≤"},
            {"pi","π"}};

            SHORTCUTS = new Dictionary<String, STRUKTURE>(){
                {"/",STRUKTURE.dijeljenje}
            };

            REPLACEENABLED = true;
        }

        public static void LOAD_FROM_CONFIGFILE()
        {
            SerijalizabilnePostavke wS = new SerijalizabilnePostavke();
            TextReader textReader = new StreamReader(settingsFileName);
            try
            {
                XmlSerializer deserializer = new XmlSerializer(typeof(SerijalizabilnePostavke));
                wS = (SerijalizabilnePostavke)deserializer.Deserialize(textReader);
                wS.loadToStatic();
            }
            catch (Exception)
            {
                Postavke.LOAD_DEFAULTS();
            }
            textReader.Close();
        }

        public static void SAVE_TO_CONFIGFILE()
        {
            TextWriter textWriter = new StreamWriter(settingsFileName);
            try
            {
                SerijalizabilnePostavke wS = new SerijalizabilnePostavke();
                wS.loadFromStatic();
                XmlSerializer serializer = new XmlSerializer(typeof(SerijalizabilnePostavke));
                serializer.Serialize(textWriter, wS);
                textWriter.Close();
            }
            catch (Exception)
            {
                textWriter.Close();
                throw new Exception("Desila se greska prilikom pohrane postavki na disk. Errcode: XML-0001");
            }

        }
    }
}
